/*******************************************************************************
        
        copyright:		Copyright (C) 2010. All Rights Reserved
        				
        license:		BSD style: $(LICENSE)
        		
        author(s): 		D implementation by Mike Reinhold and Phil DeMonaco
        				
*******************************************************************************/
module tango.util.cipher.pad.PKCS7;

import tango.util.cipher.Cipher;

/** Implementation of the PKCS7 padding scheme */
class PKCS7: BlockCipherPadding {

	PKCS7 dup() {
		return new PKCS7();
	}
	
	/***************************************************************************
	
			name
	
			Returns:
			the string name of the padding algorithm implemented
			
	***************************************************************************/
	string name() {
		return "PKCS7";
	}
	
	/***************************************************************************
	
			allowsReordering
			
			Returns:
			if the padding scheme is compatible with block reordering schemes
				
	***************************************************************************/
	bool allowsReordering(){
		return true;
	}

	/***************************************************************************
	
			pad
			
			Params:
			len = the number of padding bytes to generate
			
			Returns:
			a PKCS7 pad of the specified length
				
	***************************************************************************/
	ubyte[] pad(uint len) {
		ubyte[] pad = new ubyte[len];

		// set all pad byte values to the number of pad bytes 
		pad[0 .. len] = cast(ubyte) len;

		return pad;
	}

	/***************************************************************************
	
			unpad
			
			Params:
			input_ = the input text to find padding bytes for
			
			Returns:
			the number of PKCS7 padding bytes
				
	***************************************************************************/
	uint unpad(void[] input_) {
		auto input = cast(ubyte[]) input_;
		return cast(uint) (input[input.length - 1]); // the number of pad bytes
	}
}